<%! import cgi %>
<%inherit file="base.html"/>
<h2>
    ${profile_fields['generic']['name']['values'][0]['value']}
    <span class="inlineNav">
        % if is_friend or is_self:
            <a href="#updates">${_('Updates')}</a>
        % endif
        <a href="#mycontent">${_('Content')}</a>
        <a href="#friends">${_('Friends')}</a>
        <a href="#groups">${_('Memberships')}</a>
    </span>
</h2>
% if is_self:
    <span class="tag self">${_('You')}</span>
% elif is_friend:
    <span class="tag friend">${_('Friend')}</span>
% else:
    <span class="tag">${_('Stranger')}</span>
% endif
<div class="lastUpdated">
    ${_('Updated:')} ${profile_fields['meta']['pretty_last_updated']}
</div>
<div id="leftPanel">
    <div class="pictureWrapper">
    % if is_self and profile_fields['generic']['picture']['is_dummy']:
        <a title="${_('Click to edit image')}" href="http://${hexlabel}.hexlabel.net/person/edit_picture/">
    % elif not profile_fields['generic']['picture']['is_dummy']:
        <a title="${_('Click to view original image')}" href="${profile_fields['generic']['picture']['values'][0]['value']}">
    % endif
        <img src="${profile_fields['generic']['picture']['values'][0]['value']}" alt="${_('Profile picture')}"/>
        </a>
    % if is_self:
        <a href="/person/edit_picture" class="admin">${_('Edit')}</a>
    % endif
    </div>
    % if is_self:
        <div id="inlineInviteForm">
            % if errors:
                <div class="alert">${message}</div>
            % elif message:
                <div class="message">${message}</div>
            % endif
            % if errors_pending:
                <div class="alert">${message_pending}</div>
            % elif message_pending:
                <div class="message">${message_pending}</div>
            % endif
            <form id="invitationForm" class="inline" action="/person/" method="post">
                <p>
                    <label for="invitecode">${_('Enter invitation code:')}</label><br/>
                    <input type="text" class="text" id="invitecode" name="invitecode"/>
                    <input type="hidden" name="from_person" value="true"/>
                    <input type="submit" class="submit nowrap" name="entercode" value="${_('Go')}"/>
                </p>
            </form>
        </div>
        <div class="relatedAction">
            <a href="http://admin.hexlabel.net/invite_person/" class="invite">${_('Send an invitation to a friend')}</a>
        </div>
    % endif

    <div class="sideWidget">
        % if is_self:
            <h3>
                ${_('About')}
                <a href="/person/edit" class="admin">${_('Edit')}</a>
            </h3>
            % if profile_fields['personal']['meta']['num_set'] != 0 or profile_fields['location']['meta']['num_set'] != 0 or profile_fields['contact']['meta']['num_set'] != 0:
                % if profile_fields['personal']['meta']['num_set'] > 0:
                    <table class="personal">
                        ${displayField('personal', 'gender', 0)}
                        ${displayField('personal', 'born', 0)}
                    </table>
                % endif
                % if profile_fields['location']['meta']['num_set'] > 0:
                    <table class="location">
                        ${displayField('location', 'country', 0)}
                        ${displayField('location', 'region', 0)}
                        ${displayField('location', 'city', 0)}
                    </table>
                % endif
                % if profile_fields['contact']['meta']['num_set'] > 0:
                    <table class="contact">
                        ${displayField('contact', 'phone', 0)}
                        ${displayField('contact', 'email', 0, is_email=True, is_long=True)}
                        ${displayField('contact', 'website', 0, is_link=True, is_long=True)}
                        ${displayField('contact', 'im', 0, is_long=True)}
                    </table>
                % endif
            % else:
                <table>
                    <tr><td>${_('No profile information')}</td></tr>
                </table>
            % endif
            <span class="extraInfo"><sup>*</sup> public field</span>
        % else:
            % if profile_fields['personal']['meta']['num_visible'] or profile_fields['location']['country']['meta']['num_visible'] or profile_fields['location']['region']['meta']['num_visible'] or profile_fields['location']['city']['meta']['num_visible'] or profile_fields['contact']['meta']['num_visible']:
                <h3>
                    ${_('About')}
                </h3>
                % if profile_fields['personal']['meta']['num_visible']:
                    <table class="personal">
                        ${displayField('personal', 'gender', 0)}
                        ${displayField('personal', 'born', 0)}
                    </table>
                % endif
                % if profile_fields['location']['country']['meta']['num_visible'] or profile_fields['location']['region']['meta']['num_visible'] or profile_fields['location']['city']['meta']['num_visible']:
                    <table class="location">
                        ${displayField('location', 'country', 0)}
                        ${displayField('location', 'region', 0)}
                        ${displayField('location', 'city', 0)}
                    </table>
                % endif
                % if profile_fields['contact']['meta']['num_visible']:
                    <table class="contact">
                        ${displayField('contact', 'phone', 0)}
                        ${displayField('contact', 'email', 0, is_email=True, is_long=True)}
                        ${displayField('contact', 'website', 0, is_link=True, is_long=True)}
                        ${displayField('contact', 'im', 0, is_long=True)}
                    </table>
                % endif
            % endif
        % endif
    </div>
</div>
<div id="mainPanel">
    <div class="section" id="status">
        % if profile_fields['other']['status']['values'][0]['value']:
            <p class="status">
                <em>${profile_fields['other']['status']['meta']['label']}</em>:
                ${profile_fields['other']['status']['values'][0]['value']}
                % if profile_fields['other']['pretty_status_timestamp']['values'][0]['value']:
                    <span class="ts">${profile_fields['other']['pretty_status_timestamp']['values'][0]['value']}</span>
                % endif

                % if is_self:
                    <a href="/person/edit_status" class="admin">${_('Edit')}</a>
                % endif
            </p>
        % elif is_self:
            <p class="status">
                <em>${_('Status')}</em>: <a href="/person/edit_status">${_('Tell everyone what you are doing')}</a>
            </p>
        % endif    
    </div>
    <div class="section" id="description">
        % if profile_fields['generic']['about']['values'][0]['value']:
            <p class="intro"><% nl2br(profile_fields['generic']['about']['values'][0]['value']) %></p>
            % if is_self:
                <a href="/person/edit" class="admin">${_('Edit')}</a>
            % endif
        % endif
    </div>

    % if is_friend or is_self:
    <div class="section" id="updates">
        <h3>
            ${_('Updates')}
            <span class="inlineNav">
                % if atom_list['total'] > 4:
                    <a href="/person/updates" class="more">${_('See all...')}</a>
                % endif
            </span>
        </h3>
        % if len(events_list['items']) > 0:
            <ul>
                % for event in events_list['items'][:4]:
                    <li>
                        <a href="http://${event['source_iid']}.hexlabel.net/">${event['source_name']}</a> :
                        ${event['comment']}
                        % if event['target_iid']:
                         - <a href="http://${event['target_iid']}.hexlabel.net/">${event['target_name']}</a>
                        % endif
                        <span class="ts">${event['pretty_timestamp']}</span>
                    </li>
                % endfor
            </ul>
        % endif

        % if len(status_list['items']) > 0:
            <ul>
                % for status in status_list['items']:
                    <li>
                        <a href="http://${status['source_iid']}.hexlabel.net/">${status['source_name']}</a> :
                        ${status['comment']}
                        <span class="ts">${status['pretty_timestamp']}</span>
                    </li>
                % endfor
            </ul>
        % endif

        % if len(atom_list['items']) > 0:
            <ul class="atomList">
                % for feed in atom_list['items']:
                    % if len(feed['entries']) > 0:
                        <li class="feed">
                            <h4>${('New content in')} <a href="http://${feed['id']}.hexlabel.net/">${feed['title']}</a></h4>
                            % if feed['parent']['iid'] != hexlabel:
                                <div class="parentInfo">Linked from <a href="http://${feed['parent']['iid']}.hexlabel.net">${feed['parent']['name']}</a></div>
                            % endif
                            % for entry in feed['entries']:
                                % if entry.has_key('link'):
                                    % if entry['link'].get('type') and entry['link']['type'].startswith('image/'):
                                        <p class="inline">
                                            <img src="${entry['link']['href']}" width="32" height="32" alt=""/>
                                        </p>
                                    % else:
                                        <p class="inline">
                                            <a href="${entry['link']['href']}">${entry['title']}</a>
                                        </p>
                                    % endif
                                % else:
                                    % if entry['title']:
                                        <h5>${entry['title']}</h5>
                                    % endif
                                    <p class="entry">
                                        ${nl2br_raw(entry['summary']) | n}
                                    </p>
                                    <div class="metadata">
                                        <p class="author"><a href="${entry['author']['uri']}">${entry['author']['name']}</a></p>
                                        <p class="updated">${entry['pretty_updated']}</p>
                                    </div>
                                % endif
                            % endfor
                            <div class="clr"><br/></div>
                        </li>
                    % endif
                % endfor
            </ul>
        % endif
    </div>
    % endif

    <div class="section" id="mycontent">
        <h3>
            ${_('Content')}
            <span class="inlineNav">
                % if is_self:
                    <a href="http://admin.hexlabel.net/content/" class="admin">${_('Manage...')}</a>
                % endif
                <!--
                <a href="#wrapper">^ ${_('Top')}</a>
                -->
            </span>
        </h3>
        % if content_list['index']:
            <ul>
                % for ciid in content_list['index']:
                    % if content_list['items'][ciid]['stable']:
                        <li>
                            <a href="http://${ciid}.hexlabel.net">
                                <img src="${content_list['items'][ciid]['picture']}" class="thumbnail" alt=""/>
                            </a>
                            <a href="http://${ciid}.hexlabel.net">${content_list['items'][ciid]['title']}</a>
                            <div class="roles">
                                % if not content_list['items'][ciid]['visibility']:
                                    <span class="restricted">${_('Friends only')}</span>
                                % endif
                            </div>
                        </li>
                    % else:
                        <li class="unstable">
                            <a href="http://${ciid}.hexlabel.net">
                                <img src="${content_list['items'][ciid]['picture']}" class="thumbnail" alt=""/>
                            </a>
                            <a href="http://${ciid}.hexlabel.net">${content_list['items'][ciid]['title']}</a>
                            <p>${_('This data is not available at the moment...')}</p>
                        </li>
                    % endif
                % endfor
            </ul>
        % else:
            <ul>
                % if is_self or is_friend:
                    <li>${_('No content.')}</li>
                % else:
                    <li>${_('No public content.')}</li>
                % endif
            </ul>
        % endif
    </div>

    <div class="section" id="friends">
        <h3>
            ${_('Friends')} (${len(friend_list['index'])})
            <span class="inlineNav">
                % if is_self:
                    <a href="http://admin.hexlabel.net/invite_person/" class="admin">${_('Invite...')}</a>
                    <a href="http://admin.hexlabel.net/friends/" class="admin">${_('Manage...')}</a>
                % endif
                % if len(friend_list['index']) > 6:
                    <a href="/person/friends" class="more">${_('See all...')}</a>
                % endif
            </span>
        </h3>
        % if len(friend_list['index']) > 0:
            <ul>
                % for iid in friend_list['index'][0:6]:
                    % if friend_list['items'][iid]['stable']:
                        % if friend_list['items'][iid]['is_self']:
                        <li class="self">
                        % elif friend_list['items'][iid]['is_mutual']:
                        <li class="mutual">
                        % else:
                        <li>
                        % endif
                            <a href="http://${iid}.hexlabel.net/">
                                <img src="${friend_list['items'][iid]['picture']}" alt="" class="thumbnail"/>
                            </a>
                            <a href="http://${iid}.hexlabel.net/">
                                ${friend_list['items'][iid]['name']}
                            </a>
                            <div class="roles">
                                % if friend_list['items'][iid]['is_self']:
                                    <div class="role"><span class="self">${_('You')}</span></div>
                                % elif friend_list['items'][iid]['is_mutual']:
                                    <div class="role"><span class="mutual">${_('Mutual')}</span></div>
                                % elif is_self:
                                    <div class="role"><span class="friend">${_('Friend')}</span></div>
                                % endif
                            </div>
                        </li>
                     % else:
                        <li class="unstable">
                            <img src="${friend_list['items'][iid]['picture']}" alt="" class="thumbnail"/>
                            <a href="http://${iid}.hexlabel.net/">${friend_list['items'][iid]['name']}</a>
                            <p>${_('This data is not available at the moment...')}</p>
                        </li>
                     % endif
                % endfor
            </ul>
        % else:
            <ul>
                <li>${_('No friends')}</li>
            </ul>
        % endif
        <div class="clr"><br/></div>
    </div>

    <div class="section" id="groups">
        <h3>
            ${_('Memberships')} (${len(group_list['index'])})
            <span class="inlineNav">
                % if is_self:
                    <a href="http://admin.hexlabel.net/groups/" class="admin">${_('Manage...')}</a>
                % endif
                <!--
                <a href="#wrapper">^ ${_('Top')}</a>
                -->
            </span>
        </h3>
        % if len(group_list['index']) > 0:
            <ul>
                % for iid in group_list['index']:
                    % if group_list['items'][iid]['stable']:
                        <li>
                            % if group_list['items'][iid]['picture']:
                                <a href="http://${iid}.hexlabel.net/">
                                    <img src="${group_list['items'][iid]['picture']}" alt="" class="thumbnail"/>
                                </a>
                            % endif
                            <div class="itemContent">
                                <a href="http://${iid}.hexlabel.net/">${group_list['items'][iid]['name']}</a>
                                % if group_list['items'][iid]['is_member']:
                                    <div class="parentInfo">${_('Confirmed')}</div>
                                % endif
                            </div>
                            <div class="roles">
                                % if not group_list['items'][iid]['visibility']:
                                    <span class="restricted">${_('Friends only')}</span>
                                % endif
                                % if group_list['items'][iid]['is_mutual']:
                                    <span class="mutual">${_('Mutual')}</span>
                                % endif
                            </div>
                        </li>
                    % else:
                        <li class="unstable">
                            <a href="http://${iid}.hexlabel.net/">
                                <img src="${group_list['items'][iid]['picture']}" alt="" class="thumbnail"/>
                            </a>
                            <a href="http://${iid}.hexlabel.net/">${group_list['items'][iid]['name']}</a>
                            <p>${_('This data is not available at the moment...')}</p>
                        </li>
                    % endif
                % endfor
            </ul>
        % else:
            <ul>
                % if is_self or is_friend:
                    <li>${_('No memberships.')}</li>
                % else:
                    <li>${_('No public memberships.')}</li>
                % endif
            </ul>
        % endif
    </div>
</div>


<!-- defs -->
<%def name="title()">P2P Fusion - Peerscape | ${profile_fields['generic']['name']['values'][0]['value']}</%def>
<%def name="head()">
    <!--
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript">
        var peerscape = function() {
            return {
                init: function() {
                    // hide the invite alert message box if it exists
                    $('#notifications .collapse').hide();
                    $('#notifications #messageWrapper label')
                        .wrapInner('<a href="#"></a>')
                        .bind('click', function() {
                            $('#notifications #messageWrapper .collapse').slideDown("normal");
                            $('#notifications #messageWrapper label a')
                                .unbind('click')
                                .removeAttr('href')
                        });
                }
            }
        }();
        $(peerscape.init);
    </script>
    -->
</%def>
<%def name="body_class()">
    % if is_self:
  	    profile person self
    % elif is_friend:
  	    profile person friend
    % else:
  	    profile person
    % endif
</%def>
<%def name="extra_subtitle_content()">
    % if is_self:
        % if len(sync_sessions) == 1:
            (<a href="http://admin.hexlabel.net/#connectionsTable" title="${_('Click to see further connection details')}">${len(sync_sessions)} peer connection</a>)
        % else:
            (<a href="http://admin.hexlabel.net/#connectionsTable" title="${_('Click to see further connection details')}">${len(sync_sessions)} peer connections</a>)
        % endif
    % endif
</%def>

<%def name="notifications()">
    % if is_self and len(friend_list['index']) == 0 and len(invitations) == 0:
        <div class="item">
            ${_('If you received an invitation e-mail, please enter the invitation code in the field on the left.')}
        </div>
    % endif

    % if len(invitations) > 0 and is_self:
        % for iiid in invitations.keys():
            <div class="item">
                <p>${_('You have a pending')} 
                % if invitations[iiid]['type'] == 'group':
                    ${_('group')}
                % elif invitations[iiid]['type'] == 'principal':
                    ${_('friend')}
                % endif
                ${_('invitation')}.</p>
                <p><a href="http://${iiid}.hexlabel.net/">${_('Click here to view')}</a></p>
            </div>
        % endfor
    % endif

    % if is_empty and is_self:
        <div class="item">
            <p>${_('Your profile is very bare.')}</p>
            <p><a href="/person/edit">${_('Click here to edit your profile')}</a></p>
        </div>
    % endif
    % if not recovery_code and is_self:
        <div class="item">
            <p>${_('You have not created a Recovery Code.')}</p>
            <p><a href="http://admin.hexlabel.net/recovery_code/">${_('Click here to create a Recovery Code')}</a></p>
        </div>
    % endif

    % if invitations.has_key(hexlabel):
        <div class="item">
            <form method="post" action="http://${my_iid}.hexlabel.net/person/" class="listForm">
                <input type="hidden" name="invitation_iid" value="${hexlabel}"/>
                <dl>
                    <dd>${_('You have a pending friendship invitation from this person.')}</dd>
                    <dd class="buttons">
                        <input type="submit" class="submit" name="invitation_decline" value="${_('Decline')}"/>
                        <input type="submit" class="submit default" name="invitation_accept" value="${_('Accept')}"/>
                    </dd>
                </dl>
                <div class="clr"><br/></div>
            </form>
        </div>
    % elif not is_friend and not is_self:
        <div class="item">
            % if is_invitation_pending:
                <p>${_('You have invited this person to be your friend.')}</p>
            % else:
            <form method="post" action="http://${hexlabel}.hexlabel.net/person/invite_peerscape_person/" class="listForm" id="alertInviteForm">
                <dl>
                    <dd>${_('You are not friends with this person.')}</dd>
                    <!-- remove for now
                    <dd id="messageWrapper">
                        <input type="hidden" name="iid" value="${hexlabel}"/>
                        <label for="message">${_('Leave an optional invitation message')}</label>
                        <div class="collapse">
                            <textarea id="message" name="message" cols="40" rows="2"></textarea>
                        </div>
                    </dd>
                    -->
                    <dd class="buttons">
                        <input type="submit" class="submit" value="${_('Invite')}"/>
                    </dd>
                </dl>
                <div class="clr"><br/></div>
            </form>
            % endif
        </div>
    % endif

    % if is_self and upgrade_recommended:
        <div class="item">
            <p><a href="https://addons.mozilla.org/firefox/addon/12054">A recommended update</a> ${_(' is available.')}</p>
            <p>${_('You are currently running Peerscape version ')}${current_version}${_('.')}</p>
        </div>
    % endif

    % if is_self:
        % for ciid in content_list['items']:
            % if content_list['items'][ciid]['update']:
                <div class="item">
                    <p>${_('One or more content items have an update available.')}</p>
                    <p><a href="http://admin.hexlabel.net/content/" class="admin">${_('Click here to manage content')}</a>.</p>
                </div>
                <% break %>
            % endif
        % endfor
    % endif
</%def>

<%def name="displayField(section, field, num=0, is_email=False, is_link=False, is_long=False)">
    % if profile_fields.has_key(section) and profile_fields[section].has_key(field):
        % if (profile_fields[section][field]['values'][num]['visibility'] or is_self or is_friend) and profile_fields[section][field]['values'][num]['value']:
        <tr>
        % if is_long:
            <td class="long" colspan="2">
                % if is_self and profile_fields[section][field]['values'][num]['visibility']:
                    ${profile_fields[section][field]['meta']['label']}:<abbr title="Public field"><sup>*</sup></abbr>
                % else:
                    ${profile_fields[section][field]['meta']['label']}:
                % endif
                <span>
                    % if profile_fields[section][field]['values'][num]['value']:
                        % if is_email:
                            <a href="mailto:${profile_fields[section][field]['values'][num]['value']}">${profile_fields[section][field]['values'][num]['value']}</a>
                        % elif is_link:
                            % if profile_fields[section][field]['values'][num]['value'].startswith('http://'):
                                <a href="${profile_fields[section][field]['values'][num]['value']}" target="_blank">${profile_fields[section][field]['values'][num]['value']}</a>
                            % else:
                                <a href="http://${profile_fields[section][field]['values'][num]['value']}" target="_blank">${profile_fields[section][field]['values'][num]['value']}</a>
                            % endif
                        % else:
                            ${profile_fields[section][field]['values'][num]['value']}
                        % endif
                    % else:
                        --
                    % endif
                </span>
            </td>
        % else:
            <td class="label">
                % if is_self and profile_fields[section][field]['values'][num]['visibility']:
                    ${profile_fields[section][field]['meta']['label']}:<abbr title="Public field"><sup>*</sup></abbr>
                % else:
                    ${profile_fields[section][field]['meta']['label']}:
                % endif
            </td>
             <td class="value">
                % if profile_fields[section][field]['values'][num]['value']:
                    % if is_email:
                        <a href="mailto:${profile_fields[section][field]['values'][num]['value']}">${profile_fields[section][field]['values'][num]['value']}</a>
                    % elif is_link:
                        <a href="${profile_fields[section][field]['values'][num]['value']}" target="_blank">${profile_fields[section][field]['values'][num]['value']}</a>
                    % else:
                        ${profile_fields[section][field]['values'][num]['value']}
                    % endif
                % else:
                    --
                % endif
            </td>
        % endif
        </tr>
        % endif
    % endif
</%def>

<%def name="nl2br_raw(s)">
    ${'<br/>'.join(s.split("\n")) | n}
</%def>
<%def name="nl2br(s)">
    ${'<br/>'.join(cgi.escape(s).split("\n")) | n}
</%def>
